//
//  APIClient.swift
//  edu
//
//  Created by karlzhou on 15/11/3.
//  Copyright © 2015年 karlzhou. All rights reserved.
//

import Alamofire
import SwiftyJSON
import ObjectMapper
import Ono

class APIClient{
    
    static let PAGE_FIRST: Int = 0
    static let PAGE_SIZE: Int = 20
    
    
    
    static var token:String = "95159166ba07602a727b3e1193e3115c3fbba10ba83e6bda8dd7a7479047f2f64fd8b083f1730dce6c8d9f92eedaa806fadb9dfa85b404b069a7c77e1dbc2b37ff0d400dfd279a363cbfeb42eb5bf2a7a497b8c2111333de65f37e4201a83963469b78c1748ebfdf5ee4e1906c02cfa9"
    var headers = [
        "mob-token": token,
        "Content-Type" : "application/x-www-form-urlencoded"
    ]
    
    //let
    static func isSuccessed(result:Result<AnyObject>,failure:(code:Int,message:String)->Void) ->Bool{
        
        
        if (result.error != nil) {
            failure(code: -1, message: "网络发生异常")
            return false
        }
        
        let json=JSON(result.value!)
        if let msg=json["error_message"].string{
            failure(code: -1, message: msg)
            return false
        }
        return true
    }
    
    static func isError(error: ErrorType?, failure: (code: Int, message: String) -> Void) -> Bool {
        if (error != nil) {
            failure(code: -1, message: "网络发生异常")
            return true
        }
        return false
    }
    
    
    static func getHomeData(successed:(data:[HomeModel])->Void,failure:(code:Int,message:String)->Void){
        
        Alamofire.request(.GET, URLs.HOME_PAGE).responseString {
            (request,response,result) -> Void in
            if isError(result.error, failure: failure){
                return
            }
            let homedatas=Mapper<HomeModel>().mapArray(result.value)
            successed(data: homedatas!)
        }
    }
    
    static func getSuggestData(successed:(data:[SuggestModel])->Void,failure:(code:Int,message:String)->Void){
        
        //uuid=684110ef-8c68-3c2c-820d-a6ee23789f80&ursid=&count=8
        Alamofire.request(.GET, URLs.HOME_RECOMANDATION,parameters:["uuid": "684110ef-8c68-3c2c-820d-a6ee23789f80","count":"8"]).responseJSON {
            response -> Void in
            if isError(response.2.error, failure: failure){
                //print(response.2.error.debugDescription)
                return
            }
           // debugPrint(response)
            let homedatas=Mapper<SuggestModel>().mapArray(response.2.value!["result"])
            successed(data: homedatas!)
        }
    }
    
    static func getMovieDetail(successed:(data:Sr2Model)->Void,failure:(code:Int,message:String)->Void){
        Alamofire.request(.GET, URLs.MOVIE_DETAIL).responseString(completionHandler:{
            (request,response,result) -> Void in
            
            if(response?.statusCode != 200) {
               // print(response?.statusCode)
                return
                
            }
            
            let jsonString = result.value! as String
            
            let movieDetail = Mapper<Sr2Model>().map(jsonString)
            successed(data: movieDetail!)
            
            
            
            }
        )
        
    }
    
    static func getHotSearch(successed:(data:[HotSearchModel])->Void,failure:(code:Int,message:String)->Void){
        
        let words = SqlManager.getHotSearchWords()
        if words.count > 0 {
            successed(data: words)
            return
        }
        
        Alamofire.request(.GET, URLs.HOT_SEARCH).responseString(completionHandler: {
            (request,response,result) -> Void in
            if response?.statusCode != 200{
                failure(code: (response?.statusCode)!,message: "")
                return
            }
           
            let jsonString = result.value! as String
            
            
            let  searchWords = Mapper<HotSearchModel>().mapArray(jsonString)
            if(searchWords != nil){
                for var sw in searchWords! {
                    SqlManager.insertHotSearch(sw)
                }
                    successed(data: searchWords!)
                
            }
            else{
                failure(code: (response?.statusCode)!,message: "没有数据")
            }
            
        })
    }
}
